import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '@/views/login/index.vue'
import Layout from '@/views/layout/index.vue'
import Myorder from '@/views/myorder/index.vue'
import Search from '@/views/search/index.vue'
import SearchList from '@/views/search/list.vue'
import Pay from '@/views/pay/index.vue'
import ProdeTail from '@/views/prodetail/index.vue'

import cart from '@/views/layout/cart.vue'
import home from '@/views/layout/home.vue'
import user from '@/views/layout/user.vue'
import category from '@/views/layout/category.vue'

import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'hash',
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      redirect: '/home',
      children: [
        { path: '/home', component: home },
        { path: '/category', component: category },
        { path: '/cart', component: cart },
        { path: '/user', component: user }

      ]
    },
    { path: '/prodetail/:id', component: ProdeTail },
    { path: '/myorder', component: Myorder },
    { path: '/search', component: Search },
    { path: '/searchlist', component: SearchList },
    { path: '/pay', component: Pay }

  ]

})
const uathUrls = ['/pay', '/myorder']
router.beforeEach((to, from, next) => {
  if (!uathUrls.includes(to.path)) {
    next()
  }
  const token = store.getters.token
  if (token) {
    next()
  } else {
    next('/login')
  }
})
export default router
